package com.hymall.service.admin;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hymall.bean.*;
import com.hymall.bean.common.BasePageInfo;
import com.hymall.bean.common.CommonData;
import com.hymall.mapper.MarketAdminMapper;
import com.hymall.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author: 叶熠彬
 * @description: 系统管理 -> 管理员管理
 * @date: 2022/7/16 7:52
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    MarketAdminMapper adminMapper;


    /**
     * TODO 密码显示
     *
     * @author: 叶熠彬
     * @description: 显示管理员信息列表，
     * @date: 2022/7/16 14:41
     * @param: [username, info]
     * @return: com.hymall.bean.common.CommonData
     */
    @Override
    public CommonData queryAdminList(String username, BasePageInfo info) {
        PageHelper.startPage(info.getPage(), info.getLimit(), info.getSort() + " " + info.getOrder());
        MarketAdminExample marketAdminExample = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = marketAdminExample.createCriteria();
        if (username != null && !"".equals(username)) {
            username = "%" + username + "%";
            criteria.andUsernameLike(username);
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> marketAdmins = adminMapper.selectByExample(marketAdminExample);
        PageInfo<MarketAdmin> adminPageInfo = new PageInfo<>(marketAdmins);
        return CommonData.data(adminPageInfo);
    }


    /**
     * @author: 叶熠彬
     * @description: 添加管理员，手动添加时间信息和deleted=false
     * @date: 2022/7/16 14:51
     * @param: [admin]
     * @return: com.hymall.bean.common.CommonData
     */

    @Override
    public int addAdmin(MarketAdmin admin) {
        //先根据用户名查找是否有重复用户
        MarketAdmin marketAdmin = queryAdminByUsername(admin.getUsername());
        if (marketAdmin != null) {
            return 201;
        }
        Date date = new Date();
        admin.setPassword(Md5Util.getMd5(admin.getPassword()));
        admin.setAddTime(date);
        admin.setUpdateTime(date);
        admin.setDeleted(false);
        int i = adminMapper.insertSelective(admin);
        return 200;
    }

    /**
     * 根据用户名查找用户，返回值唯一Admin对象；
     * 不唯一或不存在时返回Null，报错
     *
     * @author: 叶熠彬
     * @date: 2022/7/16 16:33
     * @param: [username]
     * @return: com.hymall.bean.MarketAdmin
     */
    @Override
    public MarketAdmin queryAdminByUsername(String username) {
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andUsernameEqualTo(username);
        criteria.andDeletedEqualTo(false);
        List<MarketAdmin> admins = adminMapper.selectByExample(example);
        if (admins.size() != 1) {
            return null;
        }
        return admins.get(0);
    }

    /**
     * TODO 密码
     * 修改用户信息
     * 用户名不能重复
     * 密码若未修改则不update（update了不知道会不会被二次加密）
     *
     * @author: 叶熠彬
     * @date: 2022/7/16 16:46
     * @param: [admin]
     * @return: int
     */
    @Override
    public int updateAdminById(MarketAdmin admin) {
        MarketAdmin oldAdmin = adminMapper.selectByPrimaryKey(admin.getId());
        MarketAdmin existedAdmin = queryAdminByUsername(admin.getUsername());
        //根据ID判断要修改的用户名是否已经存在
        if (existedAdmin != null && !existedAdmin.getId().equals(oldAdmin.getId())) {
            return 201;
        }
        admin.setPassword(Md5Util.getMd5(admin.getPassword()));
        //密码未修改，不更新
        if (oldAdmin.getPassword().equals(admin.getPassword())) {
            admin.setPassword(null);
        }
        admin.setUpdateTime(new Date());
        MarketAdminExample example = new MarketAdminExample();
        MarketAdminExample.Criteria criteria = example.createCriteria();
        criteria.andIdEqualTo(admin.getId());
        criteria.andDeletedEqualTo(false);
        adminMapper.updateByExampleSelective(admin, example);
        return 200;
    }

    /**
     * 删除管理员账号：delete设置为true
     * TODO 自己的账号不可自己删除
     *
     * @author: 叶熠彬
     * @date: 2022/7/16 17:43
     * @param: [admin]
     * @return: int
     */
    @Override
    public int deleteAdmin(MarketAdmin admin) {
        admin.setDeleted(true);
        try {
            adminMapper.updateByPrimaryKeySelective(admin);
        } catch (Exception e) {
            e.printStackTrace();
            return 201;
        }
        return 200;
    }
}
